﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cat;
using System.Data.Odbc;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmSubcategorylist : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcConnection mycon = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        DatabaseConnection dbconn = new DatabaseConnection();
        public frmSubcategorylist()
        {
            InitializeComponent();
            this.loadData();
            linkLabel1.Click += new EventHandler(linkLabel1_Click);
            txtSearch.TextChanged += new EventHandler(txtSearch_TextChanged);
            txtSearch.Click += new EventHandler(txtSearch_Click);
            dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
        }

        void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dr = dataGridView1.SelectedRows[0];
            this.Hide();
            frmSubCategory frmS = new frmSubCategory();
            frmS.Show();
            frmS.cmbCategory.Text = dr.Cells[2].Value.ToString();
            frmS.txtSubCategoryName.Text = dr.Cells[1].Value.ToString();
            frmS.textBox1.Text = dr.Cells[0].Value.ToString();
            frmS.btnUpdate.Enabled = true;
            frmS.btnSave.Enabled = false;
        }

        void txtSearch_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
        }

        void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text != "Search Sub-Category Name")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT  * FROM tblcategory WHERE CategoryName LIKE '%" + txtSearch.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {

                loadData();

            }
        }

        void linkLabel1_Click(object sender, EventArgs e)
        {
            frmSubCategory frmS = new frmSubCategory();
            frmS.Show();
            this.Close();
        }
        void loadData()
        {
            try
            {
                dbconn.dbconnection();
                dbconn.loadproduct("Select *  from tblsubcategory");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
                dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }


        }
    }
}
